package com.spring.view_resolver;

import org.apache.poi.hssf.usermodel.*;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

@Component
public class ExcelView extends AbstractExcelView {
	protected void buildExcelDocument(
			Map<String, Object>  model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response) {
		// Go to the first sheet.
		// getSheetAt: only if workbook is created from an existing document
		// HSSFSheet sheet = workbook.getSheetAt(0);
		HSSFSheet sheet = workbook.createSheet("Spring");
		sheet.setDefaultColumnWidth(12);
		// Write a text at A1.
		HSSFCell cell = getCell(sheet, 0, 0);
		setText(cell, "Spring POI test");
		// Write the current date at A2.
		HSSFCellStyle dateStyle = workbook.createCellStyle();
		dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
		cell = getCell(sheet, 1, 0);
		setText(cell,new Date().toString());
//		cell.setCellStyle(dateStyle);
		// Write a number at A3
		getCell(sheet, 2, 0).setCellValue(458);
		// Write a range of numbers.
		HSSFRow sheetRow = sheet.createRow(3);
		for (short i = 0; i < 10; i++) {
			sheetRow.createCell(i).setCellValue(i * 10);
		}
	}

	@Override
	protected void setText(HSSFCell cell, String text) {
		cell.setCellValue(text);
	}
}
